package com.ibm.productivity.tools.blogplugin;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;

import com.ibm.productivity.tools.blogplugin.views.BlogView;
import com.ibm.productivity.tools.ui.views.RichDocumentType;
import com.ibm.productivity.tools.ui.views.RichDocumentView;
import com.ibm.productivity.tools.ui.views.RichDocumentViewSelection;
import com.sun.star.frame.XModel;
import com.sun.star.text.XText;
import com.sun.star.text.XTextDocument;
import com.sun.star.uno.UnoRuntime;

/**
 * ViewSelectionListener stores the selected RichDocumentView instance.
 * 
 * ViewSelectionListener can change the enable state of the actions on the
 * toolbar, the changing result is base on the RichDocumentView type and login
 * state : 1 : Not login yet 2 : login, a new RichDocumentView instance 3 :
 * login, a RichDocumentView instance demos the exist article content 4 : login,
 * a RichDocumentView instance demos the exist article comment See more in
 * function: <code>private void updateAction()</code>
 * 
 * ViewSelectionListener can also provide the content of the selected view. You
 * can get it throught call the function : public String getText()
 * 
 * @author TangQiao 2008-6-11
 * 
 */
public class ViewSelectionListener implements ISelectionListener {

	// Action[] actions = { loginAction, logoutAction, publishAction,
	// modifyAction, deleteAction, openArticleAction };

	RichDocumentView selectedView = null;
	BlogDoc blogdoc;
	Action[] actions;

	public void selectionChanged(IWorkbenchPart arg0, ISelection selection) {
		try {
			IAdaptable adatable = (IAdaptable) selection;
			RichDocumentViewSelection viewselection = (RichDocumentViewSelection) adatable
					.getAdapter(RichDocumentViewSelection.class);
			selectedView = viewselection.getView();
			if (selectedView.getDocumentType() == RichDocumentType.DOCUMENT_TYPE) {
				if (Configure.canUpdateAction)
					updateAction();
			} else {
				//System.out.println("type=" + selectedView.getDocumentType());
				disableAll();
			}
		} catch (Exception e) {
			disableAll();
			// will go here is you open a web browser
			// do nothing
		}

	}

	public RichDocumentView getSelctedView() {
		return selectedView;
	}

	public ViewSelectionListener(Action[] actions, BlogDoc blogdoc) {
		this.actions = actions;
		this.blogdoc = blogdoc;
		updateAction();
	}

	private void disableAll(){
		for (Action action: actions)
			action.setEnabled(false);
	}
	
	private void updateAction() {
		// not login yet
		if (blogdoc.getIsLogin() == false) {
			for (Action action : actions)
				action.setEnabled(false);
			actions[BlogView.LOGINACTION].setEnabled(true);
		}
		// login
		else {
			actions[BlogView.LOGOUTACTION].setEnabled(true);
			// is new view
			if (blogdoc.getViewList().isNewView(selectedView) == true) {
				// is new view
				if (blogdoc.getViewList().isCommentView(selectedView) == false) {
					actions[BlogView.PUBLICACTION].setEnabled(true);
					actions[BlogView.MODIFYACTION].setEnabled(false);
				} // is comment view
				else {
					actions[BlogView.PUBLICACTION].setEnabled(false);
					actions[BlogView.MODIFYACTION].setEnabled(false);
				}
			}
			// is modify view
			else {
				actions[BlogView.PUBLICACTION].setEnabled(false);
				actions[BlogView.MODIFYACTION].setEnabled(true);
			}

		}

	}

	public String getText() {
		if (selectedView == null)
			return null;
		Object obj = selectedView.getUNOModel();
		XModel model = (XModel) UnoRuntime.queryInterface(XModel.class, obj);
		XTextDocument xdoc = (XTextDocument) UnoRuntime.queryInterface(
				XTextDocument.class, model);
		XText xdocText = xdoc.getText();
		return xdocText.getString();		
	}
}
